package com.code.exam.controller;


import com.code.exam.annotation.LoginCheck;
import com.code.exam.common.result.PageResult;
import com.code.exam.common.result.Result;
import com.code.exam.dto.*;
import com.code.exam.entity.Question;
import com.code.exam.service.QuestionService;
import com.code.exam.vo.EPageResultVO;
import com.code.exam.vo.QPageResultVO;
import com.code.exam.vo.QuestionVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static io.micrometer.common.util.StringUtils.isBlank;


@Slf4j
@RestController
@RequestMapping("/admin/question")
public class QuestionController {
    @Autowired
    QuestionService questionService;


    @PostMapping("/create")
    @LoginCheck(role = "admin")
    @ApiOperation("添加试题")
    public Result addQuestion(@RequestBody  QuestionCreateDTO questionCreateDTO){

        log.info("添加试题{}",questionCreateDTO);
        if (questionCreateDTO == null) {
            return Result.error("请求数据不能为空");
        }
        if (isBlank(questionCreateDTO.getContent())) {
            return Result.error("题目内容不能为空");
        }
        if (questionCreateDTO.getOptions() == null || questionCreateDTO.getOptions().isEmpty()) {
            return Result.error("选项不能为空");
        }
        if (isBlank(questionCreateDTO.getAnswer())) {
            return Result.error("答案不能为空");
        }
        if (isBlank(questionCreateDTO.getType())) {
            return Result.error("题型不能为空");
        }
        Question question = questionService.addQuestion(questionCreateDTO);
        QuestionVO questionVO = new QuestionVO();
        BeanUtils.copyProperties(question,questionVO);
        return Result.success("试题添加成功",questionVO);
    }


    @PutMapping("/update/{id}")
    @LoginCheck(role = "admin")
    @ApiOperation("编辑试题")
    public Result update(@PathVariable("id") Long questionId, @RequestBody QuestionUpdateDTO questionUpdateDTO){
        log.info("修改试题id：{},试题内容：{}",questionId,questionUpdateDTO);
        try {
            Question question = questionService.update(questionId,questionUpdateDTO);
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(question, questionVO);
            return Result.success("试题更新成功", questionVO);
        } catch (RuntimeException e) {
            return Result.error("试题不存在或无权限修改");
        }
    }

    @DeleteMapping("/delete/{id}")
    @LoginCheck(role = "admin")
    @ApiOperation("删除试题")
    public Result delete(@PathVariable Long id){
        log.info("删除试题id：{}",id);
        questionService.delete(id);
        return Result.success("试题删除成功",id);
    }

    @GetMapping("/list")
    @LoginCheck(role = "admin")
    @ApiOperation("查询试题列表")
    public Result<PageResult<QPageResultVO>> pageQuery(QuestionPageQueryDTO questionPageQueryDTO){
        log.info("查询试题列表{}",questionPageQueryDTO);
        PageResult<QPageResultVO> pageResult = questionService.pageQuery(questionPageQueryDTO);
        return Result.success("查询成功",pageResult);
    }

    @GetMapping("/detail/{id}")
    @LoginCheck(role = "admin")
    @ApiOperation("获取单个试题详情")
    public Result getDetail(@PathVariable Long id){
        log.info("试题id{}",id);
        try {
            Question question = questionService.getDetail(id);
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(question, questionVO);
            return Result.success("获取试题成功", questionVO);
        } catch (RuntimeException e) {
            return Result.error("试题不存在");
        }
    }



}
